//
//  ItemIcon.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 11/21/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "ItemIcon.h"
#import "ItemPopup.h"
#import "UserInfo.h"
#import "MainGameScene.h"
#import "Utility.h"

@implementation ItemIcon

-(id) initWithType:(ITEM_TYPE)type
{
    mType = type;
    if (self = [super initWithNormalSprite:[CCSprite spriteWithSpriteFrameName:[self getItemFrameName]]
                            selectedSprite:nil
                            disabledSprite:nil
                                    target:self
                                  selector:@selector(onUseItem)])
    {
        CCLabelBMFont *lbNumItem = [[CCLabelBMFont alloc] initWithString:@"0" fntFile:FONT_NUMBER];
        [lbNumItem setAnchorPoint:ccp(0, 0.5)];
        [lbNumItem setPosition:uniPos(ccp(30, 10), ccp(60, 20))];
        [self addChild:lbNumItem z:0 tag:ITEM_NUM_LABEL_TAG];

        
        [lbNumItem release];
        
        if (mType == eIT_TIMER)
        {
            int baseX = 5;
            for (int i = 0; i < 3; i++)
            {
                CCSprite *spr = [CCSprite spriteWithSpriteFrameName:SFN_ITEM_II13N];
                [spr setPosition:uniPos(ccp(baseX, -4), ccp(baseX * 2, -8))];
                [self addChild:spr z:0 tag:i];
                
                CCSprite *spr2 = [CCSprite spriteWithSpriteFrameName:SFN_ITEM_II13U];
                [spr2 setPosition:spr.position];
                [spr2 setVisible:NO];
                [self addChild:spr2 z:0 tag:(i + 3)];
                
                baseX += 8;
                
            }
        }
        else
        {
            CCSprite *spr = [CCSprite spriteWithSpriteFrameName:SFN_ITEM_IIFN];
            [spr setPosition:uniPos(ccp(self.contentSize.width / 2, -4), ccp(self.contentSize.width / 2, -8))];
            [self addChild:spr z:0 tag:0];
            
            CCSprite *spr2 = [CCSprite spriteWithSpriteFrameName:SFN_ITEM_IIFU];
            [spr2 setPosition:spr.position];
            [spr2 setVisible:NO];
            [self addChild:spr2 z:0 tag:3];
            
            // expand touch
            float expand = IS_IPHONE ? 20 : 40;
            [self setActiveArea:CGRectMake(-expand, -expand, self.contentSize.width + expand * 2, self.contentSize.height + expand * 2)];
        }
    }
    
    return self;
}

-(void) onUseItem
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    if ([MainGameScene getInstance]._mainGameState == sMG_ENDING)
        return;
    
    ItemPopup *popup = [[ItemPopup alloc] initWithType:mType];
    [[CCDirector sharedDirector].runningScene addChild:popup z:Z_RESULT_POPUP];
    [popup release];

}

-(void) setNumItemUse:(int)num
{
    mNumItemUse = num;
    
    for (int i = 0; i < mNumItemUse; i++)
    {
        CCSprite *spr = (CCSprite *)[self getChildByTag:(i + 3)];
        [spr setVisible:YES];
    }
    
    for (int i = mNumItemUse; i < [self getUseMaxItem]; i++)
    {
        CCSprite *spr = (CCSprite *)[self getChildByTag:(i + 3)];
        [spr setVisible:NO];
    }
}

-(int) getNumItemUse
{
    return mNumItemUse;
}

-(NSString *) getItemFrameName
{
    switch (mType) {
        case eIT_MAGNET:
            return SFN_ITEM_MAGNET;
            
        case eIT_SHIELD:
            return SFN_ITEM_SHIELD;
            
        case eIT_TIMER:
            return SFN_ITEM_TIME;
            
        case eIT_XRAY:
            return SFN_ITEM_XRAY;
            
        default:
            return nil;
    }
}

-(void) showNumItemUse:(BOOL)isShow
{
    if (mType == eIT_TIMER)
    {
        for (int i = 0; i < 3; i++)
        {
            CCSprite *spr = (CCSprite *)[self getChildByTag:i];
            [spr setVisible:NO];
            
            CCSprite *spr2 = (CCSprite *)[self getChildByTag:(i + 3)];
            [spr2 setVisible:NO];
        }
    }
    else
    {
        CCSprite *spr = (CCSprite *)[self getChildByTag:0];
        [spr setVisible:NO];
        
        CCSprite *spr2 = (CCSprite *)[self getChildByTag:3];
        [spr2 setVisible:NO];
    }
}

-(BOOL) isUseMaxItem
{
    switch (mType) {
        case eIT_SHIELD:
            return (mNumItemUse >= ITEM_MAX_SHIELD);
            
        case eIT_MAGNET:
            return (mNumItemUse >= ITEM_MAX_MAGNET);
            
        case eIT_TIMER:
            return (mNumItemUse >= ITEM_MAX_TIMER);
            
        case eIT_XRAY:
            return (mNumItemUse >= ITEM_MAX_XRAY);
        default:
            return YES;
    }
}

-(int) getUseMaxItem
{
    switch (mType) {
        case eIT_SHIELD:
            return (ITEM_MAX_SHIELD);
            
        case eIT_MAGNET:
            return (ITEM_MAX_MAGNET);
            
        case eIT_TIMER:
            return (ITEM_MAX_TIMER);
            
        case eIT_XRAY:
            return (ITEM_MAX_XRAY);
        default:
            return 0;
    }
}

-(void) updateLabel
{
    CCLabelBMFont *lb = (CCLabelBMFont *)[self getChildByTag:ITEM_NUM_LABEL_TAG];
    if (mType == eIT_SHIELD)
    {
        [lb setString:[NSString stringWithFormat:@"%d", [UserInfo getInstance]._numItemGlove]];
    }
    else if (mType == eIT_TIMER)
    {
        [lb setString:[NSString stringWithFormat:@"%d", [UserInfo getInstance]._numItemTime]];
    }
    else if (mType == eIT_XRAY)
    {
        [lb setString:[NSString stringWithFormat:@"%d", [UserInfo getInstance]._numItemXray]];
    }
    else if (mType == eIT_MAGNET)
    {
        [lb setString:[NSString stringWithFormat:@"%d", [UserInfo getInstance]._numItemMagnet]];
    }
}

@end
